/*************************************************************************
 * Copyright 2008 David Almilli
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *      
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 **************************************************************************/
package com.tivo.hme.bananas;

import com.tivo.hme.bananas.layout.Layout;
import com.tivo.hme.sdk.Resource;
import com.tivo.hme.sdk.View;
import java.awt.Rectangle;

public class BTextPlus<E> extends BText implements PlusView {
    private boolean disposeResourceOnRemove;
    private boolean highlightInit;
    private Object font;
    private Object color;

	public BTextPlus(BView parent, int x, int y, int width, int height) {
		this(parent, x, y, width, height, true);
	}

	public BTextPlus(BView parent, int x, int y, int width, int height, boolean visible) {
		this(parent, ViewUtils.getBounds(parent, x, y, width, height), visible);
	}

    public BTextPlus(BView parent, Layout layout) {
        this(parent, layout.getBounds(), true);
    }

    public BTextPlus(BView parent, Layout layout, boolean visible) {
        this(parent, layout.getBounds(), visible);
    }

	protected BTextPlus(BView parent, Rectangle bounds, boolean visible) {
		super(parent, bounds.x, bounds.y, bounds.width, bounds.height, visible);
		setFont(getDefaultFont());
		setColor(getDefaultTextColor());
        setDisposeResourceOnRemove(true);
	}

    @Override
    public void remove(Resource animation) {
        PlusSupport.viewRemoveNotify(this);
        super.remove(animation);
    }
    
    protected void viewRemoveNotify() {
        if (getDisposeResourceOnRemove()) {
            Resource resource = getResource();
            if (resource != null) {
                //remove the resource
                resource.remove();
            }
            
            //the text view has two child views that aren't PlusViews so remove those too
            int count = getChildCount();
            for (int i=0; i < count; i++) {
                View child = getChild(i);
                if (!(child instanceof PlusView)) {
                    //remove the child resource
                    resource = child.getResource();
                    if (resource != null) {
                        resource.remove();
                    }
                }
            }
        }
    }

    public boolean getDisposeResourceOnRemove() {
        return disposeResourceOnRemove;
    }

    public void setDisposeResourceOnRemove(boolean disposeResourceOnRemove) {
        this.disposeResourceOnRemove = disposeResourceOnRemove;
    }
    
    public Rectangle getScreenFocusBounds() {
        BPoint s = toScreen();
        return new Rectangle(s.x, s.y, getWidth(), getHeight());
    }
	
	public Resource getDefaultFont() {
		BSkin skin = getBApp().getSkin();
		BSkin.Element e = skin.get(IBananasPlus.H_PARAGRAPH_FONT);
		if (e != null) {
			return e.getResource();
		} else {
			return null;
		}
	}
	
	public Resource getDefaultTextColor() {
		BSkin skin = getBApp().getSkin();
		BSkin.Element e = skin.get(IBananasPlus.H_PARAGRAPH_TEXT_COLOR);
		if (e != null) {
			return e.getResource();
		} else {
			return null;
		}
	}

    @Override
    @SuppressWarnings("unchecked")
    public E getValue() {
        return (E)super.getValue();
    }
	
    /**
     * Get the highlights.
     *
     * <p>This is a thread-safe public entry point.</p>
     */
    @Override
    public BHighlightsPlus getHighlights() {
    	if (!highlightInit) {
    		highlightInit = true;
    		super.setHighlights(new BHighlightsPlus(this));
    	}
    	return (BHighlightsPlus)super.getHighlights();
    }
    
    /**
     * Overridden to enforce the use of BHighlightsPlus.
     * @throws IllegalArgumentException if the highlights aren't an instance of BHighlightsPlus
     */
    @Override
    public void setHighlights(BHighlights highlights) throws IllegalArgumentException {
    	if (!(highlights instanceof BHighlightsPlus)) {
    		throw new IllegalArgumentException("You must use BHighlightsPlus with this view.");
    	}
    	super.setHighlights(highlights);
    }

    /**
     * Overridden to support anchoring inside the parent view.
     */
	@Override
	public void setBounds(int x, int y, int width, int height, Resource animation) {
		Rectangle bounds = ViewUtils.getBounds(getParent(), x, y, width, height);
		super.setBounds(bounds.x, bounds.y, bounds.width, bounds.height, animation);
	}
    
    public void setBounds(Layout layout, Resource animation) {
        Rectangle bounds = layout.getBounds();
        super.setBounds(bounds.x, bounds.y, bounds.width, bounds.height, animation);
    }

    public Object getFont() {
        return font;
    }

    public void setFont(Object font) {
        this.font = font;
        super.setFont(font);
    }

    public Object getColor() {
        return color;
    }

    public void setColor(Object color) {
        this.color = color;
        super.setColor(color);
    }

}
